package com.hushow.plugin.main.smain.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hushow.plugin.main.smain.dao.mapper.SmainMapper;
import com.hushow.plugin.main.smain.service.SmainService;
import com.hushow.plugin.main.smain.dao.domain.Smain;
import com.hushow.plugin.main.smain.vo.request.*;
import com.hushow.plugin.main.smain.vo.response.*;
import com.hucodex.rest.common.response.PageOutput;
import org.springframework.stereotype.Service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hucodex.rest.common.utils.LoginUtil;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * (Smain)表服务实现类
 *
 * @author hushowly
 * @since 2025-08-06 09:06:30
 */
@Service("smainService")
public class SmainServiceImpl extends ServiceImpl<SmainMapper, Smain> implements SmainService {

    @Resource
    SmainMapper smainMapper;

    /**
     * 分页查询
     *
     * @param listReq
     * @return
     */
    public PageOutput<ListSmainByPageResp> listSmainByPage(ListSmainByPageReq listReq) {
        Page<Smain> page = new Page<>(listReq.getPageNo(), listReq.getPageSize(), true);
        Smain smain = BeanUtil.copyProperties(listReq, Smain.class);
        IPage ipage = this.page(page, new LambdaQueryWrapper<>(smain).orderByDesc(Smain::getCreateTime));
        List<ListSmainByPageResp> smainVoList = null;
        if (!CollectionUtils.isEmpty(ipage.getRecords())) {
            smainVoList = BeanUtil.copyToList(ipage.getRecords(), ListSmainByPageResp.class);
        }
        return new PageOutput(ipage.getCurrent(), ipage.getSize(), ipage.getTotal(), ipage.getPages(), smainVoList);
    }

    /**
     * 查询对象
     *
     * @param id
     * @return
     */
    public SmainVo getSmainById(String id) {
        Smain smain = this.getById(id);
        if (smain == null) {
            return null;
        }
        return BeanUtil.copyProperties(smain, SmainVo.class);
    }


    /**
     * 新增指标
     *
     * @param addReq
     * @return
     */
    public SmainVo addSmain(AddSmainReq addReq) {
        Smain smain = BeanUtil.copyProperties(addReq, Smain.class);
        smain.setId(IdUtil.simpleUUID());
        smain.setCreateTime(LocalTime.now());
        smain.setUpdateTime(LocalTime.now());

        this.save(smain);
        return BeanUtil.copyProperties(smain, SmainVo.class);
    }

    /**
     * 修改指标
     *
     * @param updateReq
     * @return
     */
    public SmainVo updateSmain(UpdateSmainReq updateReq) {
        Smain smain = BeanUtil.copyProperties(updateReq, Smain.class);
        smain.setUpdateTime(LocalTime.now());
        this.updateById(smain);
        return BeanUtil.copyProperties(smain, SmainVo.class);
    }


}

